﻿

using DataService.Core.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;

namespace DataService.Core.Network
{
    public class IpHelper
    {
      //  private static readonly ICache _Cache = DictionaryCache.Instance;
        private static readonly Object lockHelper = new Object();
        private static ZipHelper? zip;
        static Boolean? _inited;
        public static string _dbFile { get; set; } = "";

        /// <summary>获取IP地址</summary>
        /// <param name="addr"></param>
        /// <returns></returns>
        public static String GetIPAddress(IPAddress addr)
        {
            if (addr == null) return "";

            if (!Init() || zip == null) return "";

            var buf = addr.GetAddressBytes();
            Array.Reverse(buf);

            var ip2 = (UInt32)DefaultConvert.ToInt(buf, 0);

            lock (lockHelper)
            {
                return zip.GetAddress(ip2) + "";
            }
        }

        static Boolean Init()
        {
            if (_inited != null) return _inited.Value;
            lock (typeof(IpHelper))
            {
                if (_inited != null) return _inited.Value;
                _inited = false;

                var z = new ZipHelper();

                if (!File.Exists(_dbFile))
                {
                    //throw new InvalidOperationException("无法找到IP数据库" + DbFile + "！");

                    return false;
                }
                //"使用IP数据库{DbFile}"
                using (var fs = File.OpenRead(_dbFile))
                {
                    try
                    {
                        z.SetStream(fs);
                    }
                    catch (Exception ex)
                    {


                        return false;
                    }
                }
                if (z.Stream != null) zip = z;
            }

            //if (zip.Stream == null) throw new InvalidOperationException("无法打开IP数据库" + DbFile + "！");

            _inited = true;
            return true;
        }

    }

    /// <summary>IP地址提供者</summary>
    public class IPProvider
    {
        /// <summary>获取IP地址的物理地址位置</summary>
        /// <param name="addr"></param>
        /// <returns></returns>
        public virtual String GetWLAddress(IPAddress addr)
        {
            // 判断局域网地址
            var ip = addr.ToString();
            var myip = NetHelper.MyIP4().ToString();
            if (ip.CutEnd(".") == myip.CutEnd(".")) return "本地局域网";

            var f = addr.GetAddressBytes()[0];
            if ((f & 0x7F) == 0) return "A类地址";
            if ((f & 0xC0) == 0x80) return "B类地址";
            if ((f & 0xE0) == 0xC0) return "C类地址";
            if ((f & 0xF0) == 0xE0) return "D类地址";
            if ((f & 0xF8) == 0xF0) return "E类地址";

            return "";
        }
    }

    //class MyIpProvider : IPProvider
    //{
    //    public override String GetWLAddress(IPAddress addr) => IpHelper.GetIPAddress(addr);
    //}
}
